this 指向当前对象,通过它可以访问当前对象的所有成员。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
class eg
{
private:
    int a;
public:
    eg(int b)
    {a=b;}
    void add (int m)
    {    
        eg p(5);
        p.a=p.a+m;
        *this=p;//this指针指向当前对象
    }
 void display()
 {cout<<a<<endl;}
};
int main()
{    
    eg q(10);
    q.display();
    q.add(10);
    q.display();//15
    return 0;
}